package com.su.dolphin.utils.ui;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.su.dolphin.R;
import com.su.dolphin.utils.ViewUtil;

public class DpTitleBar extends FrameLayout implements View.OnClickListener {

	public TextView mTxtLeft;
	public TextView mTxtRight;
	public TextView mTxtTitle;
	private TextView mTxtSubTitle;

	private LinearLayout mLinLeft = null;
	private LinearLayout mLinRight = null;

	public ImageView mImgLeft = null;
	public ImageView mImgRight = null;

	private OnLeftButtonClickListener mOnLeftButtonClickListener;
	private OnRightButtonClickListener mOnRightButtonClickListener;

	public void setLeftButton(String text, int imageId,
			OnLeftButtonClickListener listener, Integer backgroundImgId) {
		mLinLeft.setVisibility(View.VISIBLE);
		mTxtLeft.setVisibility(View.VISIBLE);
		mImgLeft.setVisibility(View.VISIBLE);
		mTxtLeft.setText(text);
		mImgLeft.setImageResource(imageId);
		mOnLeftButtonClickListener = listener;
		if (backgroundImgId != null) {
			mLinLeft.setBackgroundResource(backgroundImgId);
		}
	}

	public void setLeftButtonText(String text,
			OnLeftButtonClickListener listener, Integer textBackgroundImgId,
			Integer backgroundImgId) {
		mLinLeft.setVisibility(View.VISIBLE);
		mTxtLeft.setVisibility(View.VISIBLE);
		mImgLeft.setVisibility(View.GONE);
		mTxtLeft.setText(text);
		mOnLeftButtonClickListener = listener;
		if (textBackgroundImgId != null) {
			mTxtLeft.setBackgroundResource(textBackgroundImgId);
		}
		if (backgroundImgId != null) {
			mLinLeft.setBackgroundResource(backgroundImgId);
		}
	}

	/**
	 *
	 * @param imageId
	 *            如果为-1默认为返回键
	 * @param listener
	 * @param backgroundImgId
	 */
	public void setLeftButtonImage(int imageId,
			OnLeftButtonClickListener listener, Integer backgroundImgId) {
		mLinLeft.setVisibility(View.VISIBLE);
		mTxtLeft.setVisibility(View.GONE);
		mImgLeft.setVisibility(View.VISIBLE);
		if (imageId == -1) {
			mImgLeft.setImageResource(R.drawable.dp_titlebar_back);
		} else {
			mImgLeft.setImageResource(imageId);
		}
		mOnLeftButtonClickListener = listener;
		if (backgroundImgId != null) {
			mLinLeft.setBackgroundResource(backgroundImgId);
		}
	}

	public void removeLeftButton() {
		mLinLeft.setVisibility(View.GONE);
		mOnLeftButtonClickListener = null;
	}

	public void setRightButton(String text, int imageId,
			OnRightButtonClickListener listener, Integer backgroundImgId) {
		mLinRight.setVisibility(View.VISIBLE);
		mTxtRight.setVisibility(View.VISIBLE);
		mImgRight.setVisibility(View.VISIBLE);
		mTxtRight.setText(text);
		mImgRight.setImageResource(imageId);
		mOnRightButtonClickListener = listener;
		if (backgroundImgId != null) {
			mLinRight.setBackgroundResource(backgroundImgId);
		}
	}

	public void setRightButtonText(String text,
			OnRightButtonClickListener listener, Integer textBackgroundImgId,
			Integer backgroundImgId) {
		mLinRight.setVisibility(View.VISIBLE);
		mTxtRight.setVisibility(View.VISIBLE);
		mImgRight.setVisibility(View.GONE);
		mTxtRight.setText(text);
		mOnRightButtonClickListener = listener;
		if (textBackgroundImgId != null) {
			mTxtRight.setBackgroundResource(textBackgroundImgId);
		}
		if (backgroundImgId != null) {
			mLinRight.setBackgroundResource(backgroundImgId);
		}
	}

	public void setRightButtonImage(int imageId,
			OnRightButtonClickListener listener, Integer backgroundImgId) {
		mLinRight.setVisibility(View.VISIBLE);
		mTxtRight.setVisibility(View.GONE);
		mImgRight.setVisibility(View.VISIBLE);
		mImgRight.setImageResource(imageId);
		mOnRightButtonClickListener = listener;
		if (backgroundImgId != null) {
			mLinRight.setBackgroundResource(backgroundImgId);
		}
	}

	public void removeRightButton() {
		mLinRight.setVisibility(View.GONE);
		mOnRightButtonClickListener = null;
	}

	public DpTitleBar(Context context) {
		this(context, null);
	}

	public DpTitleBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DpTitleBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// LayoutInflater inflater = (LayoutInflater)
		// context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// inflater.inflate(R.layout.layout_dp_titlebar, this, true);
		ViewUtil.inflateView(R.layout.layout_dp_titlebar, this, true);
		mLinLeft = (LinearLayout) findViewById(R.id.dp_title_lin_left);
		mLinRight = (LinearLayout) findViewById(R.id.dp_title_lin_right);

		mImgLeft = (ImageView) findViewById(R.id.dp_title_img_left);
		mImgRight = (ImageView) findViewById(R.id.dp_title_img_right);

		mTxtLeft = (TextView) findViewById(R.id.dp_title_txt_left);
		mTxtRight = (TextView) findViewById(R.id.dp_title_txt_right);

		mTxtTitle = (TextView) findViewById(R.id.dp_title_txt_title);
		mTxtSubTitle = (TextView) findViewById(R.id.dp_title_txt_subtitle);

		mLinLeft.setOnClickListener(this);
		mLinRight.setOnClickListener(this);

		// mLinLeft.setVisibility(View.GONE);
		mImgLeft.setImageResource(R.drawable.dp_titlebar_back);
		mLinRight.setVisibility(View.GONE);
		mTxtTitle.setVisibility(View.GONE);
		mTxtTitle.setMaxWidth(ViewUtil.dip2px(200));
		mTxtTitle.setSingleLine();
	}

	public void setTitle(CharSequence text) {
		mTxtTitle.setVisibility(View.VISIBLE);
		mTxtTitle.setText(text);
	}

	public void setTitle(int stringID) {
		mTxtTitle.setVisibility(View.VISIBLE);
		mTxtTitle.setText(stringID);
	}

	public void setSubTitle(String text) {
		mTxtSubTitle.setVisibility(View.VISIBLE);
		mTxtSubTitle.setText(text);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.dp_title_lin_left) {
			if (mOnLeftButtonClickListener != null)
				mOnLeftButtonClickListener.onLeftBtnClick(v);
			else {
				if (getContext() instanceof Activity) {
					((Activity) getContext()).finish();
				}
			}
		} else if (id == R.id.dp_title_lin_right) {
			if (mOnRightButtonClickListener != null)
				mOnRightButtonClickListener.onRightBtnClick(v);
		}
	}

	public void setBackgroundColor(int color) {
		setBackgroundColor(color);
	}

	public void setBackgroundResource(int resId) {
		setBackgroundResource(resId);
	}

	public interface OnLeftButtonClickListener {
		public void onLeftBtnClick(View button);
	}

	public interface OnRightButtonClickListener {
		public void onRightBtnClick(View button);
	}

}
